package view.component.monster.large_monster
{
	import enums.main.UISize;
	
	import feathers.controls.List;
	import feathers.controls.Screen;
	import feathers.controls.Scroller;
	import feathers.controls.renderers.DefaultListItemRenderer;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.data.ListCollection;
	import feathers.layout.HorizontalLayout;
	import feathers.layout.TiledRowsLayout;
	import feathers.layout.VerticalLayout;
	
	import model.vo.monster.MonsterListVo;
	
	import starling.display.Image;
	import starling.events.Event;
	
	import view.event.monster.TypeListViewEvent;
	import view.render.monster.LTypeListItem;
	
	
	/**
	 * 鸟龙种分类列表Screen
	 * @author £iang
	 * 
	 */	
	public class TypeListView extends Screen
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 分类数据表 */
		public var monsterList:List;
		
		/** 分类 */
		public var monsterType:String;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 列表数据 */
		private var _listData:ListCollection;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			var listHeight:Number;
			
			monsterList = new List();
			this.addChild( monsterList );
			monsterList.width = UISize.MAIN_W;
			monsterList.height = listHeight = UISize.MAIN_H - 
				( UISize.MONSTER_TAB_AREA_H + UISize.LMONSTER_LIST_TAB_AREA_H ) * this.dpiScale;
			monsterList.itemRendererProperties.accessoryField  = "item";
			monsterList.itemRendererFactory = itemRenderFactroy;

			var tileLayout:TiledRowsLayout = new TiledRowsLayout();
			var columnCount:int = int( UISize.MAIN_W / ( UISize.MONSTER_LIST_ITEM_W * this.dpiScale ) );
			var rowCount:int = int( listHeight / ( UISize.MONSTER_LIST_ITEM_H * this.dpiScale ) );
			
			tileLayout.useSquareTiles = false; 
			tileLayout.tileVerticalAlign = VerticalLayout.VERTICAL_ALIGN_TOP;
			tileLayout.tileHorizontalAlign = HorizontalLayout.HORIZONTAL_ALIGN_LEFT;
			tileLayout.verticalAlign = VerticalLayout.VERTICAL_ALIGN_TOP;
			tileLayout.horizontalAlign = HorizontalLayout.HORIZONTAL_ALIGN_LEFT;
			tileLayout.horizontalGap =  ( UISize.MAIN_W - columnCount * UISize.MONSTER_LIST_ITEM_W * this.dpiScale ) / ( columnCount - 1 );
			tileLayout.verticalGap =   ( listHeight - rowCount * UISize.MONSTER_LIST_ITEM_H * this.dpiScale ) / ( rowCount - 1 );
			monsterList.layout = tileLayout;
			monsterList.snapToPages = true;
			monsterList.scrollerProperties.scrollBarDisplayMode = Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;

			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			var centerX:Number = monsterList.width / 2;
			var centerY:Number = monsterList.height / 2;
			
			this.pivotX = this.x = centerX;
			this.pivotY = this.y = centerY;
			
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		public function addListener():void
		{
			monsterList.addEventListener( Event.CHANGE, listSelectedHandler );
		}
		
		/**
		 * 设置列表数据 
		 * @param dataVec 数据
		 * 
		 */		
		public function setListData( dataVec:Vector.<MonsterListVo> ):void
		{
			var i:int;
			var len:int = dataVec.length;
			var tmpObj:Object;
			var tmpImage:Image;
			
			_listData = new ListCollection([]);
			
			for ( i = 0; i < len; i ++ )
			{
				var item:LTypeListItem = new LTypeListItem();
				var tmpData:MonsterListVo = dataVec[ i ] as MonsterListVo;
				
				item.setData( tmpData );
				_listData.push( { item:item, itemData:tmpData } );
			}
			
			monsterList.dataProvider = _listData;
		}
		
		/**
		 * itemRenderer皮肤 
		 * @return 
		 * 
		 */		
		private function itemRenderFactroy():IListItemRenderer
		{
			var renderer:DefaultListItemRenderer = new DefaultListItemRenderer();
			
			renderer.width = UISize.MONSTER_LIST_ITEM_W * this.dpiScale;
			renderer.height = UISize.MONSTER_LIST_ITEM_H * this.dpiScale;
			renderer.accessoryGap = 0;
			renderer.gap = 0;
			renderer.accessoryPosition = "left";
			renderer.itemHasIcon = false;
			renderer.itemHasLabel = false;
			return renderer;
		}

		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理选中列表某一项后派发的事件 
		 * @param event 选中列表某一项后派发的事件 
		 * 
		 */		
		protected function listSelectedHandler( event:Event ):void
		{
			if ( monsterList.selectedIndex >= 0 )
			{
				var typeListViewEvent:TypeListViewEvent;
				
				if ( monsterType.charAt( 0 ) == "l" )
				{
					typeListViewEvent = new TypeListViewEvent(
						TypeListViewEvent.READ_SELECT_LDETAIL );
				}
				else
				{
					
				}
				
				typeListViewEvent.curReadMonsterId = ( _listData.data[ monsterList.selectedIndex ].itemData as MonsterListVo ).monsterId;
				dispatchCommonEvent( typeListViewEvent );
				monsterList.selectedIndex = -1;
			}
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			disposeItems();
			monsterList.dispose();
			monsterList = null;
			super.dispose();
		}
		
		
		/**
		 * 析构列表renderer 
		 * 
		 */		
		public function disposeItems():void
		{
			if ( _listData )
			{
				for ( var i:int = 0; i < _listData.length; i ++ )
				{
					if ( _listData.data[ i ].itemData )
					{
						_listData.data[ i ].itemData = null;
					}
					
					_listData.data[ i ].item.dispose();
					_listData.data[ i ].item = null;
				}
				
				_listData.removeAll();
				_listData = null;
			}
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function TypeListView()
		{
			super();
		}
	}
}